﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using FonShare.Classes.Data;
using FonShare.Classes;

namespace FonShare
{
    public partial class JoinGroup : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                int userId = (int) Session[FS_Constants.Session.USERID];
                FS_DataClassesDataContext dc = new FS_DataClassesDataContext();
                List<FS_Group> groupsList = dc.FS_Groups.Where(record => record.OwnerID != userId).ToList();
                foreach (FS_Group currentGroup in groupsList)
                {
                    GroupsListBox.Items.Add(new ListItem(currentGroup.GroupName + " (" + currentGroup.FS_GroupUsers.Count + ")", currentGroup.GroupID.ToString()));
                }
            }
        }

        protected void JoinGroupsButton_Click(object sender, EventArgs e)
        {
            if (GroupsListBox.SelectedItem != null)
            {
                FS_GroupUser newGroupUser = new FS_GroupUser();
                newGroupUser.GroupID = Int32.Parse(GroupsListBox.SelectedItem.Value);
                newGroupUser.UserID = (int)Session[FS_Constants.Session.USERID];
                newGroupUser.IsActive = false;
                FS_DataClassesDataContext dc = new FS_DataClassesDataContext();
                dc.FS_GroupUsers.InsertOnSubmit(newGroupUser);
                dc.SubmitChanges();
            }
        }
    }
}
